package admin

import (
	"context"
	"fmt"
	"github.com/kataras/iris/v12/mvc"
	"micro-grpc-server/app/common"
	"micro-grpc-server/app/http/controllers"
	pb "micro-grpc-server/services/user/proto"
)

type UserController struct {
	controllers.Controller
	UserClient pb.UserService
}

func (c *UserController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("GET", "/list", "List")
}

// List
// @Router /admin/user/list [get]
// @Summary 用户列表
// @Description 用户列表
// @Tags Admin - User
// @Produce json
// @Param query query user.ListUserRequest false "搜索的数据"
// @Success 200 {array} user.User
func (c *UserController) List(ctx context.Context) {
	req := &pb.ListUserRequest{}
	if err := c.ReadQuery(req); err != nil {
		c.BadRequest(err.Error())
		return
	}
	res, err := c.UserClient.UserList(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败 -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.JsonSucMeta(res.List, common.Extra{
		Count: res.Total,
		Page:  c.QueryPage(),
	})
}
